<template>
  <view class="container">
    <view>
      <view style="padding: 10px;"></view>
      <view v-for="(item, idx) in listWeiwancheng" :key="item.id">
        <view class="card" @click="toAuditWeiwancheng(item.id)">
          <view class="card-title">
            <uni-row>
              <uni-col :span="24">
                <view style="font-weight: bold;">
                  <uni-icons type="wallet-filled" size="18"></uni-icons>
                  {{item.timeName}}
                </view>
              </uni-col>
            </uni-row>
          </view>
          <view class="card-body">
            {{item.content}}
            <br />
          </view>
          <view class="card-footer">
            <uni-row>
              <uni-col :span="24">
                <view
                  style="text-align: right;color: #999999;"
                >{{getFormatedTimeStr(item.createTime)}}</view>
              </uni-col>
            </uni-row>
          </view>
        </view>

        <view style="padding-bottom: 10px;"></view>
        <!-- <view style="padding-top: 30px;"></view> -->
      </view>
      <uni-load-more :status="moreStatusWeiwancheng"></uni-load-more>
    </view>
    <view style="padding-top: 30px;"></view>
    <button type="primary" @click="clickApply" style="margin-bottom:10px">新增周报</button>
  </view>
</template>

<script>
import { websock } from "../../net/websock";
import { getWeeklyList } from "../../net/weeklyWork";
import moment from "moment";

export default {
  data() {
    return {
      listWeiwancheng: [],
      pageSize: 10,
      pageNum: 1,
      total: 1, //当前页
      moreStatusYiwancheng: "more",
      moreStatusWeiwancheng: "more"
    };
  },
  onShow() {
    this.getNewestTasksWeiwancheng(true);
  },
  onLoad() {},
  onUnload() {},
  onReady() {},
  // 下拉刷新，获取最新的数据
  onPullDownRefresh() {
    this.pageNum = 1;
    console.log("onPullDownRefresh");
    this.getNewestTasksWeiwanchengs();
  },
  // 上拉刷新，获取历史数据
  onReachBottom() {
    uni.showLoading({
      title: "加载中"
    });
    if (this.total <= this.pageNum) {
      uni.hideLoading();
      uni.showToast({
        title: "没有更多了",
        duration: 2000,
        icon: "none"
      });
      return;
    }
    this.pageNum += 1;
    this.getNewestTasksWeiwancheng(true);
  },
  methods: {
    clickApply() {
      uni.navigateTo({
        url: "/pages/my/addWeeklyWork"
      });
    },
    toAuditWeiwancheng(id) {
      console.log("audit weiwancheng", id, this.listWeiwancheng);
      uni.navigateTo({
        url: `/pages/my/deleteWeek?id=${id}`
      });
    },
    // 获取最新
    async getNewestTasksWeiwancheng(show) {
      if (show) {
        this.listData = [];
        this.pageNum = 1;
        this.pageSize = 10;
      }
      try {
        let that = this;
        const params = { pageNum: this.pageNum, pageSize: this.pageSize };
        console.log(params);
        let res = await getWeeklyList(params);
        console.log(res);
        setTimeout(function() {
          uni.hideLoading();
          uni.stopPullDownRefresh();
          that.listWeiwancheng = res.data;
          that.total = res.total;
        }, 500);
      } catch (err) {
        console.error(err);
        uni.showToast({ title: err.message || "error" });
      }
    },
    // 获取最新
    async getNewestTasksWeiwanchengs(show) {
      if (show) {
        this.listData = [];
        this.pageNum = 1;
        this.pageSize = 10;
      }
      try {
        let that = this;
        if (this.pageNum === 1) this.listWeiwancheng = [];
        const params = { pageNum: this.pageNum, pageSize: this.pageSize };
        console.log(params);
        let res = await getWeeklyList(params);
        console.log(res);
        setTimeout(function() {
          uni.hideLoading();
          uni.stopPullDownRefresh();
          that.listWeiwancheng = that.listWeiwancheng.concat(res.data);
          that.total = res.total;
        }, 500);
      } catch (err) {
        console.error(err);
        uni.showToast({ title: err.message || "error" });
      }
    },
    // 获取格式化时间字符串
    getFormatedTimeStr(stamp, str = "YYYY-MM-DD HH:mm:ss") {
      return moment(stamp).format(str);
    }
  }
};
</script>

<style>
@import url("../common/common.css");
</style>
